﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;

namespace pingstat
{
    public partial class editcfg : Form
    {
        public editcfg()
        {
            InitializeComponent();
        }

        public string[,] cfg = new string[20, 2];

        private void editcfg_Load(object sender, EventArgs e)
        {
            int i = 0;
            XmlReader pingconfig = new XmlTextReader("config.xml");
            while (pingconfig.Read())
            {
                if (pingconfig.NodeType == System.Xml.XmlNodeType.Element)
                {
                    if (pingconfig.Name == "nm")
                    {
                        pingconfig.Read();
                        cfg[i, 0] = pingconfig.Value;
                    }
                    if (pingconfig.Name == "ip")
                    {
                        pingconfig.Read();
                        cfg[i, 1] = pingconfig.Value;
                        i++;
                    }
                }
            }
            if (cfg[0, 1] == "NONE")
            {
                textBox2.Enabled = false;
                checkBox1.Checked = true;
            }
            if (cfg[1, 1] == "NONE")
            {
                textBox6.Enabled = false;
                checkBox3.Checked = true;
            }
            if (cfg[2, 1] == "NONE")
            {
                textBox10.Enabled = false;
                checkBox5.Checked = true;
            }
            if (cfg[3, 1] == "NONE")
            {
                textBox14.Enabled = false;
                checkBox7.Checked = true;
            }
            if (cfg[4, 1] == "NONE")
            {
                textBox18.Enabled = false;
                checkBox9.Checked = true;
            }
            if (cfg[5, 1] == "NONE")
            {
                textBox22.Enabled = false;
                checkBox11.Checked = true;
            }
            if (cfg[6, 1] == "NONE")
            {
                textBox26.Enabled = false;
                checkBox13.Checked = true;
            }
            if (cfg[7, 1] == "NONE")
            {
                textBox30.Enabled = false;
                checkBox15.Checked = true;
            }
            if (cfg[8, 1] == "NONE")
            {
                textBox34.Enabled = false;
                checkBox17.Checked = true;
            }
            if (cfg[9, 1] == "NONE")
            {
                textBox38.Enabled = false;
                checkBox19.Checked = true;
            }
            if (cfg[10, 1] == "NONE")
            {
                textBox4.Enabled = false;
                checkBox2.Checked = true;
            }
            if (cfg[11, 1] == "NONE")
            {
                textBox8.Enabled = false;
                checkBox4.Checked = true;
            }
            if (cfg[12, 1] == "NONE")
            {
                textBox12.Enabled = false;
                checkBox6.Checked = true;
            }
            if (cfg[13, 1] == "NONE")
            {
                textBox16.Enabled = false;
                checkBox8.Checked = true;
            }
            if (cfg[14, 1] == "NONE")
            {
                textBox20.Enabled = false;
                checkBox10.Checked = true;
            }
            if (cfg[15, 1] == "NONE")
            {
                textBox24.Enabled = false;
                checkBox12.Checked = true;
            }
            if (cfg[16, 1] == "NONE")
            {
                textBox28.Enabled = false;
                checkBox14.Checked = true;
            }
            if (cfg[17, 1] == "NONE")
            {
                textBox32.Enabled = false;
                checkBox16.Checked = true;
            }
            if (cfg[18, 1] == "NONE")
            {
                textBox36.Enabled = false;
                checkBox18.Checked = true;
            }
            if (cfg[19, 1] == "NONE")
            {
                textBox40.Enabled = false;
                checkBox20.Checked = true;
            }
            textBox1.Text = cfg[0, 0];
            textBox2.Text = cfg[0, 1];
            textBox5.Text = cfg[1, 0];
            textBox6.Text = cfg[1, 1];
            textBox9.Text = cfg[2, 0];
            textBox10.Text = cfg[2, 1];
            textBox13.Text = cfg[3, 0];
            textBox14.Text = cfg[3, 1];
            textBox17.Text = cfg[4, 0];
            textBox18.Text = cfg[4, 1];
            textBox21.Text = cfg[5, 0];
            textBox22.Text = cfg[5, 1];
            textBox25.Text = cfg[6, 0];
            textBox26.Text = cfg[6, 1];
            textBox29.Text = cfg[7, 0];
            textBox30.Text = cfg[7, 1];
            textBox33.Text = cfg[8, 0];
            textBox34.Text = cfg[8, 1];
            textBox37.Text = cfg[9, 0];
            textBox38.Text = cfg[9, 1];
            textBox3.Text = cfg[10, 0];
            textBox4.Text = cfg[10, 1];
            textBox7.Text = cfg[11, 0];
            textBox8.Text = cfg[11, 1];
            textBox11.Text = cfg[12, 0];
            textBox12.Text = cfg[12, 1];
            textBox15.Text = cfg[13, 0];
            textBox16.Text = cfg[13, 1];
            textBox19.Text = cfg[14, 0];
            textBox20.Text = cfg[14, 1];
            textBox23.Text = cfg[15, 0];
            textBox24.Text = cfg[15, 1];
            textBox27.Text = cfg[16, 0];
            textBox28.Text = cfg[16, 1];
            textBox31.Text = cfg[17, 0];
            textBox32.Text = cfg[17, 1];
            textBox35.Text = cfg[18, 0];
            textBox36.Text = cfg[18, 1];
            textBox39.Text = cfg[19, 0];
            textBox40.Text = cfg[19, 1];
        }

        private void button1_Click(object sender, EventArgs e)
        {
            FileStream fs = new FileStream("config.xml", FileMode.OpenOrCreate);
            fs.SetLength(10);
            XmlTextWriter savecfg = new XmlTextWriter(fs, Encoding.UTF8);
            cfg[0, 0] = textBox1.Text;
            cfg[0, 1] = textBox2.Text;
            cfg[1, 0] = textBox5.Text;
            cfg[1, 1] = textBox6.Text;
            cfg[2, 0] = textBox9.Text;
            cfg[2, 1] = textBox10.Text;
            cfg[3, 0] = textBox13.Text;
            cfg[3, 1] = textBox14.Text;
            cfg[4, 0] = textBox17.Text;
            cfg[4, 1] = textBox18.Text;
            cfg[5, 0] = textBox21.Text;
            cfg[5, 1] = textBox22.Text;
            cfg[6, 0] = textBox25.Text;
            cfg[6, 1] = textBox26.Text;
            cfg[7, 0] = textBox29.Text;
            cfg[7, 1] = textBox30.Text;
            cfg[8, 0] = textBox33.Text;
            cfg[8, 1] = textBox34.Text;
            cfg[9, 0] = textBox37.Text;
            cfg[9, 1] = textBox38.Text;
            cfg[10, 0] = textBox3.Text;
            cfg[10, 1] = textBox4.Text;
            cfg[11, 0] = textBox7.Text;
            cfg[11, 1] = textBox8.Text;
            cfg[12, 0] = textBox11.Text;
            cfg[12, 1] = textBox12.Text;
            cfg[13, 0] = textBox15.Text;
            cfg[13, 1] = textBox16.Text;
            cfg[14, 0] = textBox19.Text;
            cfg[14, 1] = textBox20.Text;
            cfg[15, 0] = textBox23.Text;
            cfg[15, 1] = textBox24.Text;
            cfg[16, 0] = textBox27.Text;
            cfg[16, 1] = textBox28.Text;
            cfg[17, 0] = textBox31.Text;
            cfg[17, 1] = textBox32.Text;
            cfg[18, 0] = textBox35.Text;
            cfg[18, 1] = textBox36.Text;
            cfg[19, 0] = textBox39.Text;
            cfg[19, 1] = textBox40.Text;
            savecfg.Formatting=Formatting.Indented;
            savecfg.WriteStartDocument();
            savecfg.WriteStartElement("serverlist");
            for (int i = 0; i < 20; i++)
            {
                savecfg.WriteStartElement("srv");
                savecfg.WriteElementString("nm", cfg[i, 0]);
                savecfg.WriteElementString("ip", cfg[i, 1]);
                savecfg.WriteEndElement();
            }
            savecfg.WriteEndElement();
            savecfg.WriteEndDocument();
            savecfg.Flush();
            fs.Close();
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
            {
                textBox2.Text = "NONE";
                textBox2.Enabled = false;
            }
            else
            {
                textBox2.Text = "10.190.200.142";
                textBox2.Enabled = true;
            }
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox2.Checked)
            {
                textBox4.Text = "NONE";
                textBox4.Enabled = false;
            }
            else
            {
                textBox4.Text = "10.190.200.142";
                textBox4.Enabled = true;
            }
        }

        private void checkBox3_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox3.Checked)
            {
                textBox6.Text = "NONE";
                textBox6.Enabled = false;
            }
            else
            {
                textBox6.Text = "10.190.200.142";
                textBox6.Enabled = true;
            }
        }

        private void checkBox4_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox4.Checked)
            {
                textBox8.Text = "NONE";
                textBox8.Enabled = false;
            }
            else
            {
                textBox8.Text = "10.190.200.142";
                textBox8.Enabled = true;
            }
        }

        private void checkBox5_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox5.Checked)
            {
                textBox10.Text = "NONE";
                textBox10.Enabled = false;
            }
            else
            {
                textBox10.Text = "10.190.200.142";
                textBox10.Enabled = true;
            }
        }

        private void checkBox6_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox6.Checked)
            {
                textBox12.Text = "NONE";
                textBox12.Enabled = false;
            }
            else
            {
                textBox12.Text = "10.190.200.142";
                textBox12.Enabled = true;
            }
        }

        private void checkBox7_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox7.Checked)
            {
                textBox14.Text = "NONE";
                textBox14.Enabled = false;
            }
            else
            {
                textBox14.Text = "10.190.200.142";
                textBox14.Enabled = true;
            }
        }

        private void checkBox8_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox8.Checked)
            {
                textBox16.Text = "NONE";
                textBox16.Enabled = false;
            }
            else
            {
                textBox16.Text = "10.190.200.142";
                textBox16.Enabled = true;
            }
        }

        private void checkBox9_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox9.Checked)
            {
                textBox18.Text = "NONE";
                textBox18.Enabled = false;
            }
            else
            {
                textBox18.Text = "10.190.200.142";
                textBox18.Enabled = true;
            }
        }

        private void checkBox10_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox10.Checked)
            {
                textBox20.Text = "NONE";
                textBox20.Enabled = false;
            }
            else
            {
                textBox20.Text = "10.190.200.142";
                textBox20.Enabled = true;
            }
        }

        private void checkBox11_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox11.Checked)
            {
                textBox22.Text = "NONE";
                textBox22.Enabled = false;
            }
            else
            {
                textBox22.Text = "10.190.200.142";
                textBox22.Enabled = true;
            }
        }

        private void checkBox12_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox12.Checked)
            {
                textBox24.Text = "NONE";
                textBox24.Enabled = false;
            }
            else
            {
                textBox24.Text = "10.190.200.142";
                textBox24.Enabled = true;
            }
        }

        private void checkBox13_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox13.Checked)
            {
                textBox26.Text = "NONE";
                textBox26.Enabled = false;
            }
            else
            {
                textBox26.Text = "10.190.200.142";
                textBox26.Enabled = true;
            }
        }

        private void checkBox14_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox14.Checked)
            {
                textBox28.Text = "NONE";
                textBox28.Enabled = false;
            }
            else
            {
                textBox28.Text = "10.190.200.142";
                textBox28.Enabled = true;
            }
        }

        private void checkBox15_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox15.Checked)
            {
                textBox30.Text = "NONE";
                textBox30.Enabled = false;
            }
            else
            {
                textBox30.Text = "10.190.200.142";
                textBox30.Enabled = true;
            }
        }

        private void checkBox16_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox16.Checked)
            {
                textBox32.Text = "NONE";
                textBox32.Enabled = false;
            }
            else
            {
                textBox32.Text = "10.190.200.142";
                textBox32.Enabled = true;
            }
        }

        private void checkBox17_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox17.Checked)
            {
                textBox34.Text = "NONE";
                textBox34.Enabled = false;
            }
            else
            {
                textBox34.Text = "10.190.200.142";
                textBox34.Enabled = true;
            }
        }

        private void checkBox18_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox18.Checked)
            {
                textBox36.Text = "NONE";
                textBox36.Enabled = false;
            }
            else
            {
                textBox36.Text = "10.190.200.142";
                textBox36.Enabled = true;
            }
        }

        private void checkBox19_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox19.Checked)
            {
                textBox38.Text = "NONE";
                textBox38.Enabled = false;
            }
            else
            {
                textBox38.Text = "10.190.200.142";
                textBox38.Enabled = true;
            }
        }

        private void checkBox20_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox20.Checked)
            {
                textBox40.Text = "NONE";
                textBox40.Enabled = false;
            }
            else
            {
                textBox40.Text = "10.190.200.142";
                textBox40.Enabled = true;
            }
        }
    }
}
